import './public-path'
import 'assets/css/main.scss'
import App from './App.vue'
import routes from './router'
import store from './store'
import appStore from './appStore'

import * as api from '@/api' // API接口
Vue.prototype.$api = api

// elementUI初始化
Vue.use(ELEMENT, { zIndex: '3000' })

import components from './components'

Vue.use(components)

Vue.config.productionTip = false

let router = null
let instance = null

function render({ data = {}, container } = {}) {
  router = new VueRouter({
    base: window.__POWERED_BY_QIANKUN__ ? '/anchor' : '/',
    mode: 'history',
    routes
  })
  instance = new Vue({
    ...App,
    router,
    store,
    data() {
      return {
        parentRouter: data.router,
        parentVuex: data.store
      }
    }
  }).$mount(container ? container.querySelector('#app') : '#app')
}

if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

export async function bootstrap() {
  console.log('child-one app bootstraped')
}

export async function mount(props) {
  console.log('props from main framework', props.data)
  props.data.store.commit('global/SET_HEADERSHOW', true)
  appStore(props)
  render(props)
}

export async function unmount() {
  instance.$destroy()
  instance.$el.innerHTML = ''// 解决子项目内容泄露问题
  instance = null
  router = null
}
